
var PlayLayer = cc.Layer.extend({
        bgSprite : null, 
    score : 0,
    horse_count : 10,
    map_icon : new Map(),   //创建icon map
    railing : null,
    horse_list : null, 
    count_down : 3,         //倒计时  
    current_count : 0, 
    game_state : 0,         //游戏状态  0： 未开始  1：进行中 2：暂停
    dn : null,
    over_count : 0,
    horse_size : 20,
    railing_points : null,
    horse_move_time : 10,
    debug : 1,
    onEnter : function() {
            this._super();
            this.initHorse();
            this.initRailing();
    },
    ctor:function(){
                this._super();

                var size = cc.winSize;

                //添加背景
                this.bgSprite = new cc.Sprite(res.bg);
                this.bgSprite.attr({
                        x: size.width / 2,
                        y: size.height / 2,
                });

                this.addChild(this.bgSprite, 0);


                //添加分数
                this.scoreLabel = new cc.LabelTTF("score:0", "", 20);
                this.scoreLabel.attr({
                        x: size.width - 60,
                        y: size.height  - 20,
                        color: cc.color(0, 0, 255),
                });
                this.addChild(this.scoreLabel, 20);

                //添加倒计时标签
                this.count_down_lable = new cc.LabelTTF("", "", 80);
                this.count_down_lable.attr({
                        x : size.width / 2,
                        y : size.height / 2,
                        color : cc.color(255,0, 0), 
                });
                this.addChild(this.count_down_lable, 1000);

                //添加logo
                var logo = new cc.Sprite(res.logo);
                logo.attr({
                        x: size.width - 35,
                        y: 15,
                });
                logo.setScale(0.5);
                this.addChild(logo, 20);

                this.horse_list = [];
                this.railing_points = [];

                this.current_count = this.count_down + 1;
                this.schedule(this.onTimer, 1, this.count_down, 0);   
                this.game_state = 0;

                this.dn = new cc.DrawNode();
                this.addChild(this.dn, 10);


                //添加tap标志
                this.tapLable = new cc.LabelTTF("", "", 20);
                this.tapLable.attr({ 
                        x: 60,
                        y: size.height - 20,
                        color : cc.color(255, 0, 0),
                });
                this.addChild(this.tapLable, 10);

                return true;
    },

    updatetap : function(){
            if (this.railing.tapping){
                    this.tapLable.setString("Tap");
            }
            else {
                    this.tapLable.setString("No Tap"); 
            }
    },

    onTimer :function () {
            this.current_count -= 1;
            console.log(this.current_count);
            if (this.current_count == 0) {
                    //开始
                    this.game_state = 1;
                    this.start();
                    this.count_down_lable.setString("Go!");

                    var ac = new cc.FadeOut(1);
                    this.count_down_lable.runAction(ac);
            }
            else {
                    this.count_down_lable.setString(this.current_count);
            }

    },

    start : function() {
            this.over_count = 0;
            for(var i = 0; i < this.horse_list.length; i ++){
                    this.horse_list[i].go();
            } 
            this.scheduleUpdate(); 
    },

    update : function() {
            for(var i = 0; i < this.horse_list.length; i ++){ 
                    if (this.horse_list[i].is_running == 0){
                            continue;
                    }

                    var t_rect = this.horse_list[i].getBoundingBox();
                    for(var j = 0; j < this.railing_points.length; j ++) {
                            //挨个检查是否碰撞了
                            if (cc.rectContainsPoint(t_rect, this.railing_points[j])) { 
                                    this.horse_list[i].stopAllActions();
                                    this.horse_list[i].is_running = 0;
                                    this.addscore(1);
                                    this.addOverCount(this.horse_list[i]);
                                    break; //
                            }
                    }
            } 
    }, 

    addscore : function(score) {
            this.score += score;
            this.scoreLabel.setString("score:" + this.score); 
    },

    initHorse : function(){
            var size = cc.winSize;

            //批量添加草泥马
            for(var i = 0; i < this.horse_count; i ++) {
                    var horse = new HorseSprite(res.horse);
                    horse.attr({
                            x: size.width / 2 + (cc.random0To1() - 0.5) * size.width / 5, 
                            y: size.height/ 2 + (cc.random0To1() - 0.5) * size.height/ 5, 
                    });
                horse.setScale(0.2);
                horse.radiu = size.height;
                horse.index = i;
                horse.move_time = this.horse_move_time;
                this.addChild(horse, 5);
                this.horse_list.push(horse);
                
                //this.horse_size = 20; //设置马匹大小
        } 
    },

    addOverCount : function(target) {
            this.over_count ++; 
            if (this.over_count == this.horse_count){ 
                    this.onOver();
            }
    },

    onOver: function(){
            //输出结果
            var gameOver = new cc.LayerColor(cc.color(0,0,0,150));
            var size = cc.winSize;
            var titleLabel = new cc.LabelTTF("游戏结束! " , "Arial", 38);
            titleLabel.attr({
                    x:size.width / 2 , 
                    y:size.height -  60, 
                    color: cc.color(255,255,0),
            }); 
            gameOver.addChild(titleLabel, 10);


            var touchlistener = cc.EventListener.create({
                    event : cc.EventListener.TOUCH_ONE_BY_ONE,
                swallowTouches: true,
                onTouchBegan : function(touch, event) {
                        console.log("gameOver onTouchBegan...");
                        return true;            
                },  
            }); 

            /***********************************************************/
            //展示结果页
            var resultLabel = new cc.LabelTTF(this.score + "分,击败了1%的网友！", 
                            "", 20,cc.size(size.width - 50, 200), cc.TEXT_ALIGNMENT_CENTER);  
            resultLabel.attr({
                    x: size.width / 2,
                    y: size.height - 200,
                    color : cc.color(255,0,0),
            }); 
            gameOver.addChild(resultLabel, 10);



            /***************************************************/
            //重试菜单
            var TryAgainItem = new cc.MenuItemFont(
                            "重试",
                            function () {
                                    var s = new PlayScene();
                                    s.debug = this.debug;
                                    s.horse_move_time = this.horse_move_time;
                                    s.horse_count = this.horse_count;
                                    cc.director.runScene(s);
                            }, this);
            TryAgainItem.attr({
                    x: size.width/2 + 50,
                    y: 50, 
                    anchorX: 0.5,
                    anchorY: 0.5
            });

            /***************************************************/
            //回退菜单
            var BackItem = new cc.MenuItemFont(
                            "返回",
                            function() { 
                                    cc.director.popScene();
                            }, this);
            BackItem.attr({
                    x: size.width / 2 - 50,
                    y: 50,
                    anchorX: 0.5,
                    anchorY: 0.5,
            });

            var menuItems = [TryAgainItem, BackItem/*, ShareItem*/];
            var menu = new cc.Menu(menuItems);
            menu.x = 0;
            menu.y = 0;
            menu.setColor(cc.color(255,0,0));


            gameOver.addChild(menu, 1);
            //添加浮层
            this.getParent().addChild(gameOver);


            console.log("onOver"); 
    },
    
    initRailing : function(){
            var size = cc.winSize; 
            this.railing = new RailingSprite(res.railing);
            this.railing.attr({
                    x: size.width / 4,
                    y: size.height / 4,
                    opacity : 200,
            });
            this.railing.setScale(0.5);
            this.railing.debug = this.debug;
            this.addChild(this.railing, 15);
            this.railing.addTouchEventListense(); 
           

    },
});


var PlayScene = cc.Scene.extend ({
        horse_move_time : 10,         
        horse_count : 10, 
        debug : 0,
        onEnter:function() {
                this._super();
                var layer = new PlayLayer();
                layer.horse_move_time = this.horse_move_time;
                layer.horse_count = this.horse_count;
                layer.debug = this.debug;
                this.addChild(layer); 

        }
});
